<?php
//require_once dirname(__FILE__).'../config.php';
//引入ManagersModel.class.php和StudentModel.class.php
require_once dirname(__FILE__).'/../model/ManagersModel.class.php';
require_once dirname(__FILE__).'/../model/StudentModel.class.php';
/* 
 *登录
 */
class LoginController{
	//两种身份
	private $managersModel = null;
	private $studentModel = null;
	
	public function login(){
		//获取表单提交的参数(account、password以及indentity)
		$account = isset($_POST['account']) ? $_POST['account'] : '';
		$password = isset($_POST['password']) ? $_POST['password'] : '';
		$identity = isset($_POST['identity']) ? $_POST['identity'] : '';
		//echo "*******"."$identity"."99999";
		//调用下层验证(首先判断学生身份或是管理员)
		//学生身份
		if($identity === 'student'){
			//实例化
 			$studentModel = new StudentModel();
			//账号正确返回true,不正确返回错误信息
			$login = $studentModel->login($account, $password);
			if($login === true){
				define('APP', 'itacast');
				//启动Session
				session_start();
				$_SESSION['studentInfo'] = array(
						'studentId' => $account,
						'studentPassword' => $password
				);
				//跳转到学生界面
				header("Location:http://localhost/SelectCoursesSystem/view/student/StudentView.php");
				//require Root_Path.'/view/student/StudentView.php';
				//结束脚本
				die;
			} else {
				//输出错误信息
				echo $login;
				//返回登录界面
				header("Location:http://localhost/SelectCoursesSystem/RegisterLogin.php");
				//require Root_Path.'/RegisterLogin.php';
				//结束脚本s
				die;
			}
			
		} else if($identity === 'manager'){	//管理员身份
			//实例化
			$managerModel = new ManagersModel();
			//账号正确返回密码和权限,不正确返回错误信息
			$login = $managerModel->login($account, $password);
/*  		echo "login = ";
			var_dump($login);  */
			if(!empty($login[0]['manager_password'])){
				define('APP', 'itcast');
				//echo '登录中.......';
				//启动Session
				session_start();
				$_SESSION['managerInfo'] = array(
					'managerId' => $account,
					'managerPassword' => $password
				);
				//未通过审核的管理员
				if($login[0]['right_control'] === '0'){
					//返回登录界面
					header("Location:http://localhost/SelectCoursesSystem/RegisterLogin.php");
				}	else if($login[0]['right_control'] === '50'){
					//普通管理员
					//跳转到管理员界面
					header("Location:http://localhost/SelectCoursesSystem/view/manager/ManagerView.php");
					//require './view/manager/ManagerView.php';					
				} else if($login[0]['right_control'] === '100'){
					//超级管理员
					header("Location:http://localhost/SelectCoursesSystem/view/manager/SuperManagerView.php");
				}
				//结束脚本
				die;
			} else {
				//输出错误信息
				echo $login;
				//返回登录界面
				//require Root_Path.'/RegisterLogin.php';
				header("Location:http://localhost/SelectCoursesSystem/RegisterLogin.php");
				//结束脚本
				die;
			}
		}
		
	}
	
}
	//执行登录
	$loginController = new loginController();
	$login = $loginController->login();
	//echo "login = ".$login;

?>